package com.lpdemilis.pokermanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ForgotPasswordActivity extends Activity implements MyAsyncListener {
	protected CustomHttpClient client;
	protected String SERVER_URL;

	protected EditText usernameEditText;
	protected EditText emailEditText;
	protected Button sendButton;
	protected Button cancelButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forgotpassword);

		if (Build.MANUFACTURER.equals("unknown")) {
			SERVER_URL = this.getString(R.string.URL_local);
		} else {
			SERVER_URL = this.getString(R.string.URL);
		}
		
		TextView activityTitleTextView = (TextView) findViewById(R.id.activityTitleTextView);
		Typeface titleTypeface = Typeface.createFromAsset(getAssets(),
                "fonts/POKERPN.TTF");        
        activityTitleTextView.setTypeface(titleTypeface);

		usernameEditText = (EditText) findViewById(R.id.usernameEditText);
		emailEditText = (EditText) findViewById(R.id.emailEditText);
		sendButton = (Button) findViewById(R.id.sendButton);
		cancelButton = (Button) findViewById(R.id.cancelButton);

		sendButton.setOnClickListener(sendButtonClickListener);
		cancelButton.setOnClickListener(cancelButtonClickListener);
	}

	public enum ActionsEnum {
		sendPassword, error;
	}

	public enum ErrorsEnum {
		no_user_error, no_email_error, change_confirmation_email, error;
	}

	@Override
	public void executeReturn(String returnValue) {
		String[] returnArray;
		returnArray = returnValue.split("##");

		ActionsEnum action;

		try {
			action = ActionsEnum.valueOf(returnArray[0]);
		} catch (Exception e) {
			action = ActionsEnum.error;
		}

		switch (action) {
		case sendPassword:
			sendPasswordReturn(returnArray);
			break;
		case error:
			errorReturn(returnArray);
			break;
		}
	}

	private void sendPasswordReturn(String[] returnArray) {
		if (returnArray.length <= 1) {
			errorReturn(returnArray);
		} else {
			if (returnArray[1].trim().equals("")) {
				AlertDialog alertDialog = new AlertDialog.Builder(
						ForgotPasswordActivity.this).create();
				alertDialog.setTitle(R.string.forgot_password_email_title);
				alertDialog.setMessage(ForgotPasswordActivity.this
						.getString(R.string.forgot_password_email));
				alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
						ForgotPasswordActivity.this
								.getString(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								cancelButton.performClick();
							}
						});

				alertDialog.show();
			} else {
				ErrorsEnum errors;

				try {
					errors = ErrorsEnum.valueOf(returnArray[1]);
				} catch (Exception e) {
					errors = ErrorsEnum.error;
				}

				String errorTitle;
				String errorMessage;

				switch (errors) {
				case no_user_error:
					errorTitle = this.getString(R.string.error);
					errorMessage = this.getString(R.string.no_user_error);
					break;
				case no_email_error:
					errorTitle = this.getString(R.string.error);
					errorMessage = this.getString(R.string.no_email1_error);
					break;	
				case change_confirmation_email:
					errorTitle = this.getString(R.string.error);
					errorMessage = this
							.getString(R.string.change_confirmation_email_error);
					break;
				default:
					errorTitle = this.getString(R.string.error);
					errorMessage = this.getString(R.string.error);
					break;
				}

				AlertDialog alertDialog = new AlertDialog.Builder(
						ForgotPasswordActivity.this).create();
				alertDialog.setTitle(errorTitle);
				alertDialog.setMessage(errorMessage);
				alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
						ForgotPasswordActivity.this
								.getString(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								return;
							}
						});

				alertDialog.show();
			}
		}
	}

	private void errorReturn(String[] returnArray) {
		AlertDialog alertDialog = new AlertDialog.Builder(
				ForgotPasswordActivity.this).create();
		alertDialog.setTitle(R.string.error);
		alertDialog.setMessage(ForgotPasswordActivity.this
				.getString(R.string.error));
		alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
				ForgotPasswordActivity.this.getString(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});

		alertDialog.show();
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ForgotPasswordActivity.this,
				LoginActivity.class);
		ForgotPasswordActivity.this.startActivity(intent);
		ForgotPasswordActivity.this.finish();
	}

	View.OnClickListener sendButtonClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			AlertDialog alertDialog = new AlertDialog.Builder(
					ForgotPasswordActivity.this).create();
			alertDialog.setTitle(R.string.error);

			if ((usernameEditText.getText().toString().trim().equals("") && emailEditText
					.getText().toString().trim().equals(""))
					|| (!usernameEditText.getText().toString().trim()
							.equals("") && !emailEditText.getText().toString()
							.trim().equals(""))) {
				alertDialog.setMessage(ForgotPasswordActivity.this
						.getString(R.string.user_or_password_error));
				alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "OK",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								usernameEditText.requestFocus();
							}
						});

				alertDialog.show();
				return;
			}

			sendPassword();
		}
	};

	public void sendPassword() {
		try {
			client = new CustomHttpClient(this,
					this.getString(R.string.wait_message), true);
			String action = ActionsEnum.sendPassword.toString();
			String paramName1 = "username";
			String paramValue1 = usernameEditText.getText().toString();
			String paramName2 = "email";
			String paramValue2 = emailEditText.getText().toString();
			client.execute(SERVER_URL, action, paramName1, paramValue1,
					paramName2, paramValue2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	View.OnClickListener cancelButtonClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(ForgotPasswordActivity.this,
					LoginActivity.class);
			ForgotPasswordActivity.this.startActivity(intent);
			ForgotPasswordActivity.this.finish();
		}
	};
}
